<template>
	<div class="sw_table_container">
		<el-table size="medium" :data="data" height="100%" class="index_table_wrapper">
			<el-table-column type="index" label="序号" align="center" width="56"></el-table-column>
			<el-table-column prop="userName" label="PI名称"></el-table-column>
			<el-table-column prop="filed10" label="PI外链" min-width="200"></el-table-column>
			<el-table-column label="PI上级" min-width="120" :formatter="piNameFormatter"></el-table-column>
			<el-table-column prop="createName" label="创建人" width="120"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
			<el-table-column label="操作" width="164" v-if="$isHasAuth(tableButton)">
				<template slot-scope="{ row }">
					<el-button type="text" v-hasPermi="'f5c80d2d6af44b1baba29a68fa24e2d1'" @click="handleOpenLink(row)">{{ !row.filed10 ? '添加' : '修改' }}外链</el-button>
					<el-button type="text" v-hasPermi="'021cf8f47b87485ea1a965b1e9dcb62d'" @click="handleOpenSuperior(row)">{{ !row.filed9 ? '添加' : '修改' }}上级</el-button>
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>

<script>
import piSelect from './piSelect';
import configMixins from '../configMixins';
import { updateUser } from '@/api/system/tbSysUser';
export default {
	mixins: [configMixins],
	components: { piSelect },
	computed: {
		tableButton() {
			return ['f5c80d2d6af44b1baba29a68fa24e2d1', '021cf8f47b87485ea1a965b1e9dcb62d'];
		}
	},
	methods: {
		piNameFormatter(data) {
			let info = this.data.find((n) => n.id === data.filed9);
			return !info ? '--' : info.userName;
		},

		handleOpenLink(data) {
			let that = this;
			that.$prompt(`请输入PI审核员【${data.userName}】的外链`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				inputValue: data.filed10,
				inputPattern: /^(http|https):\/\/(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]+)[/\?\:]?.*$/,
				inputErrorMessage: '请输入有效的URL地址',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						let params = { id: data.id, filed10: instance.inputValue };
						instance.confirmButtonLoading = true;
						await that.updateUserRequest(params);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		handleOpenSuperior(data) {
			let that = this;
			let checkedPI = data.filed9;
			that.$msgbox({
				title: '请选择上级pi审核员',
				message: that.$createElement('piSelect', {
					style: { width: '100%' },
					attrs: { list: that.data },
					props: { value: checkedPI },
					on: {
						input(val) {
							checkedPI = val;
						}
					}
				}),
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						let params = { id: data.id, filed9: checkedPI };
						instance.confirmButtonLoading = true;
						await that.updateUserRequest(params);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//更新用户数据请求
		async updateUserRequest(params) {
			let result = await updateUser(params).then((res) => res.data);
			if (result.code === '0') {
				let dialogTitle = '';
				if (!!params.filed9) {
					dialogTitle = '更新PI上级';
				} else if (params.filed10) {
					dialogTitle = '更新PI外链';
				}
				this.$emit('refresh');
				this.$showCustomMessage('success', `${dialogTitle}成功`);
			}
		}
	}
};
</script>

<style scoped></style>
